package com.cloud.servlet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.core.BaseServlet;
import com.cloud.dao.MyPatientBean;
import com.cloud.dao.PatientBean;
import com.cloud.service.FileService;
import com.cloud.service.LableService;
import com.cloud.service.MyPatientService;
import com.cloud.util.Code;
import com.cloud.util.CommUtil;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2015-12-7
 * @version 1.0 
 */
@Controller
@RequestMapping("/my/patient")
public class MyPatientServlet extends BaseServlet {
	private static final Logger logger = LoggerFactory.getLogger(MyPatientServlet.class);
	@Autowired
	private MyPatientService MyPatientService;
	@Autowired
	private FileService FileService;
	@Autowired
	private LableService LabletService;
	
	/**
	 * 查询我的病人信息
	 * @param request
	 * @param mypatient
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET})
	public @ResponseBody Object list(HttpServletRequest request, MyPatientBean mypatient) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(mypatient == null || (CommUtil.isEmpty(mypatient.getDoc_id()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		logger.debug(String.format("开始查询医生[%s][%s]的我的病人的信息", mypatient.getDoc_id(), mypatient.getClientType()));
		List<PatientBean> retPatientList = new ArrayList<PatientBean>();
		mypatient.setStatus("0");
		if(CommUtil.isEmpty(mypatient.getClientType())){
			mypatient.setClientType("2"); //医生
		}
		retPatientList = MyPatientService.getMyPatientList(mypatient);
		List<Map<String, String>> _mapList = new ArrayList<Map<String, String>>();
		for(PatientBean _patient : retPatientList){
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("id", _patient.getId());
			_map.put("userName", _patient.getUserName());
			_map.put("fullName", _patient.getFullname());
			_map.put("head", FileService.getVisitUrl(_patient.getCrc()));
			_map.put("note", _patient.getGender()); //备注
			_map.put("proId", _patient.getPro_id());
			_map.put("proName", _patient.getPro_name());
			_map.put("joinDate", _patient.getJoin_date());
			_map.put("createTime", _patient.getCreateTime());
			_map.put("hos_id", _patient.getHos_id());
			if(!"-1".equals(_patient.getPro_id())){
				_map.put("hos_id", MyPatientService.getHosByPatient(_patient));
			}
			_mapList.add(_map);
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", _mapList);
		return map;
	}
	
	/**
	 * 添加我的患者信息
	 * @param request
	 * @param mypatient
	 * @return
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST})
	public @ResponseBody Object add(HttpServletRequest request, @RequestBody MyPatientBean mypatient) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(mypatient == null || CommUtil.isEmpty(mypatient.getDoc_id()) || CommUtil.isEmpty(mypatient.getPat_id())){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		mypatient.setStatus("0");
		mypatient.setPro_id("-1");
		MyPatientService.addMyPatient(mypatient);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 修改我的患者备注信息
	 * @param request
	 * @param mypatient
	 * @return
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST})
	public @ResponseBody Object update(HttpServletRequest request, @RequestBody MyPatientBean mypatient) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(mypatient == null || CommUtil.isEmpty(mypatient.getDoc_id()) || CommUtil.isEmpty(mypatient.getPat_id()) || CommUtil.isEmpty(mypatient.getNote())){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		try {
			MyPatientService.updateMyPatient(mypatient);
		} catch (Exception e) {
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 删除我的患者
	 * @param request
	 * @param mypatient
	 * @return
	 */
	@RequestMapping(value = "/delete", method = { RequestMethod.POST})
	public @ResponseBody Object delete(HttpServletRequest request, @RequestBody MyPatientBean mypatient) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(mypatient == null || CommUtil.isEmpty(mypatient.getDoc_id()) || CommUtil.isEmpty(mypatient.getPat_id())){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		MyPatientService.deleteMyPatient(mypatient);
		LabletService.deletePatientLable(mypatient);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
}
